//
//  HMEmoticonManager.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

let emoticonsOfPage = 20

let HMEmoticonColumnOfPage = 7

let HMEmoticonRowOfPage = 3

let emoticonsBundlePath = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!

class HMEmoticonManager: NSObject {
    
    static let sharedManager: HMEmoticonManager = HMEmoticonManager()
    
    private let emoticonPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! + "/emoticons.plist"
    
    lazy var packageModels: [HMEmoticonSectionModel] = self.loadSections()
    
    private func loadSections() -> [HMEmoticonSectionModel]
    {
        let recentSection = HMEmoticonSectionModel(id: "", group_name_cn: "最近", emoticons: loadRecentEmoticon())
        let defaultSection = loadSection("com.sina.default")
        let emojiSection = loadSection("com.apple.emoji")
        let lxhSection = loadSection("com.sina.lxh")
        
        return [recentSection,defaultSection,emojiSection,lxhSection]
    }
    
    private func loadSection(id: String) ->HMEmoticonSectionModel
    {

        let infoPath = emoticonsBundlePath + "/" + id + "/" + "info.plist"
        
        let infoDict = NSDictionary(contentsOfFile: infoPath)!
        
        let  group_name_cn = infoDict["group_name_cn"] as! String
        
        var emoticons = [HMEmoticonModel]()
        
        if let emoticonsArray = infoDict["emoticons"] as? [[String: String]]
        {
            for dict in emoticonsArray
            {
                let model = HMEmoticonModel(dict: dict, id: id)
                emoticons.append(model)
            }
        }
        return HMEmoticonSectionModel(id: id, group_name_cn: group_name_cn, emoticons: emoticons)
    }
    
    ///添加到最近表情
    func addRecentEmoticon(emoticon: HMEmoticonModel)
    {
//        获得最近这也的表情
        var recentPageEoticons = packageModels[0].pageEmoticons[0]
//        记录相同的表情
        var sameEmoticon: HMEmoticonModel? = nil
//        判断是否存在一样的表情
        for e in recentPageEoticons
        {
            if (emoticon.emoji != nil && emoticon.code == e.code) || (emoticon.chs != nil && emoticon.chs == e.chs)
            {
                sameEmoticon = e
                break
            }
        }
        
//        如果存在一样的表情就先删除
        if sameEmoticon != nil
        {
            let index = recentPageEoticons.indexOf(sameEmoticon!)!
            recentPageEoticons.removeAtIndex(index)
        }
        
//        将模型添加到最前面
        recentPageEoticons.insert(emoticon, atIndex: 0)
        
//        如果表情数量超过20,删除后面的表情
        while recentPageEoticons.count > 20
        {
            recentPageEoticons.removeLast()
        }
        
//        swift中的数组和OC的不一样,值传递,会复制元素,需要再次赋值回去
        packageModels[0].pageEmoticons[0] = recentPageEoticons
        
//        保存到本地
        saveRecentEmoticons()
    }
    
    /// 保存表情
    private func saveRecentEmoticons()
    {
        NSKeyedArchiver.archiveRootObject(packageModels[0].pageEmoticons[0], toFile: emoticonPath)
    }
    
//    MARK: - 加载最近表情包
    private func loadRecentEmoticon() ->[HMEmoticonModel]
    {
        if let emoticons = NSKeyedUnarchiver.unarchiveObjectWithFile(emoticonPath) as? [HMEmoticonModel]
        {
            print("加载到了表情!")
            return emoticons
        }else
        {
            return [HMEmoticonModel]()
        }
    }
}








